package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.RecommendUser;
import com.itheima.service.mongo.RecommendUserService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class RecommendUserServiceimpl implements RecommendUserService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public RecommendUser findMaxScoreRecommendUser(Long toUserId) {
        //根据投诉而id获得推荐用户，构建条件
        Query query = new Query(Criteria.where("toUserId").is(toUserId)).with(Sort.by(Sort.Order.desc("score"))).skip(0).limit(1);
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        return recommendUser;
    }

    @Override
    public PageBeanVo findRecommendUserList(Long touserId, Integer pageNum, Integer pageSize) {
        //构建条件
        Integer indext =(pageNum - 1) * pageSize + 1;
        Query query = Query.query(Criteria.where("toUserId").is(touserId)).with(Sort.by(Sort.Order.desc("score"))).skip(indext).limit(pageSize);
        //获得该id的所有推荐好友
        List<RecommendUser> recommendUserList = mongoTemplate.find(query, RecommendUser.class);
        //查询总数
        long totol = mongoTemplate.count(query,RecommendUser.class);
        return new PageBeanVo(pageNum,pageSize,totol,recommendUserList);
    }
//获得佳人的详细信息
    @Override
    public RecommendUser findRecommendUserByCondition(Long toUserId, Long userId) {
        // 1.构建条件
        if (userId==toUserId) {
            RecommendUser recommendUser = new RecommendUser();
            recommendUser.setUserId(userId);
            recommendUser.setScore(100d);
            return recommendUser;
        }
        Query query = Query.query(Criteria.where("toUserId").is(toUserId).and("userId").is(userId));
        // 2.查询一条
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        // 3.返回
        return recommendUser;
    }
}
